package com.example.robertluozizhao.framecollectdemo.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.event.EventBusMessageEvent;
import com.example.robertluozizhao.framecollectdemo.event.observer.MyObserable;

import org.greenrobot.eventbus.EventBus;

import java.util.Observable;
import java.util.Observer;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by robert.luozizhao on 2018/1/24.
 */

public class EventPostActivity extends AppCompatActivity implements Observer{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_event);
        ButterKnife.bind(this);
        MyObserable.getInstance().addObserver(this);
    }


    @OnClick({R.id.post_null, R.id.post_int, R.id.post_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.post_null:
                EventBusMessageEvent eventBusMessageEvent = new EventBusMessageEvent();
                eventBusMessageEvent.setMessage("事件01");
                EventBus.getDefault().post(eventBusMessageEvent);
                break;
            case R.id.post_int:
                MyObserable.getInstance().setChanged();
                MyObserable.getInstance().notifyObservers("自定义观察者");
                break;
            case R.id.post_back:

                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        Toast.makeText(this,arg.toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyObserable.getInstance().deleteObserver(this);
    }
}
